﻿using LearnHelper;
using SimpleBuffer;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SimpleBufferDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            DefaultByteBuffer buffer = new DefaultByteBuffer(1024);
            buffer.IncrementSize = 256;
            var bytes = new byte[800];
            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = (byte)(i % byte.MaxValue);
            }
            int count = 1000000;
            Stopwatch watch = new Stopwatch();
            var t1 = StopwatchHelper.Milliseconds(() =>
            {
                for (int i = 0; i < count; i++)
                {
                    buffer.Write(bytes);
                    if (buffer.ReadableLength > 0)
                    {
                        var reads = buffer.Read(buffer.ReadableLength);
                        watch.Start();
                        Task.Factory.StartNew((data) =>
                        {
                            var arr = data as byte[];
                            for (int j = 0; j < arr.Length; j++)
                            {
                                if (arr[j] != (byte)(j % byte.MaxValue))
                                {
                                    Console.WriteLine("data error!");
                                }
                            }
                        }, reads);
                        watch.Stop();
                    }
                }
            });
            Console.WriteLine("run {0}, use time: {1}ms, new task use time: {2}ms", count, t1 - watch.ElapsedMilliseconds, watch.ElapsedMilliseconds);
            Console.ReadLine();
        }
    }
}
